package team.scau.laboratory_repair.web.sys;

import com.querydsl.core.QueryResults;
import lombok.extern.apachecommons.CommonsLog;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import team.scau.laboratory_repair.common.base.BaseController;
import team.scau.laboratory_repair.common.json.JqGridResult;
import team.scau.laboratory_repair.common.page.MyPage;
import team.scau.laboratory_repair.common.utils.JsonTool;
import team.scau.laboratory_repair.modules.po.QRole;
import team.scau.laboratory_repair.modules.po.Role;
import team.scau.laboratory_repair.service.RoleService;

import java.util.HashMap;
import java.util.Map;


/**
 * @author 30254
 * creadtedate:2018/8/19
 */
@Controller
@CommonsLog
@RequestMapping("/pc/sys/role")
public class RoleController extends BaseController {

    @Autowired
    RoleService roleService;

    @ModelAttribute("role")
    public Role get(@RequestParam(required = false, value = "id") Long id) throws Exception {
        if (id != null && id > 0) {
            return roleService.findById(id);
        } else {
            return new Role();
        }
    }

    @RequiresPermissions("sys:role:list")
    @GetMapping(value = "/list", produces = "text/html;charset=utf-8")
    public String list(Model model) throws Exception{
        return "/pc/sys/role/list";
    }

    @ResponseBody
    @RequiresPermissions("sys:role:list")
    @RequestMapping(value = "/list.json", produces = "application/json;charset=utf-8")
    public JqGridResult list() throws Exception{
        MyPage page = getPage(QRole.class);
        QueryResults<Role> roles = roleService.list(page);
        JqGridResult result = new JqGridResult(roles);
        return result;
    }

    @RequiresPermissions("sys:role:edit")
    @GetMapping(value = "/edit", produces = "text/html;charset=utf-8")
    public String edit(@ModelAttribute("role") Role role, Model model) throws Exception{
        model.addAttribute("role",role);
        return "/pc/sys/role/edit";
    }

    @RequiresPermissions("sys:role:perm")
    @GetMapping(value = "/perm", produces = "text/html;charset=utf-8")
    public String perm(@ModelAttribute("role") Role role, Model model) throws Exception{
        model.addAttribute("permTree", JsonTool.toJson(roleService.selectPermTree(role.getPerm(),getCUser())));
        return "/pc/sys/role/perm";
    }

    /**
     * 更新角色信息
     * @param role
     * @return
     * @throws Exception
     */
    @RequiresPermissions(value = {"sys:role:edit","sys:role:perm","sys:role:seller"}, logical = Logical.OR)
    @PostMapping(value = "/edit", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> edit(@ModelAttribute Role role){
        Map<String,Object> result = new HashMap<>();
        try {
            roleService.save(role);
            result.put("status",200);
            result.put("msg","角色信息保存成功");
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage(), e);
            result.put("status",500);
            result.put("msg","角色信息保存失败");
        }
        return result;
    }

    /**
     * 删除角色
     * @param id
     * @return
     * @throws Exception
     */
    @RequiresPermissions("sys:role:delete")
    @PostMapping(value = "/delete", produces = "application/json;charset=utf-8")
    @ResponseBody
    public Map<String,Object> delete(Long id){
        Map<String,Object> result = new HashMap<>();
        try {
            if(id != null && id > 0){
                roleService.deleteById(id);
                result.put("status",200);
                result.put("msg","删除角色成功");
            }else {
                result.put("status",500);
                result.put("msg","请选择删除的角色");
            }
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage(), e);
            result.put("status",500);
            result.put("msg","删除角色失败");
        }
        return result;
    }

}
